<template>
	<view>
		<view class="head">
			<view class="size">已上传文件(已使用空间{{capacity}}M/500M)</view>
			<view class="history" @click="toHistory">打印记录</view>
		</view>
		<view class="file_list">
			<checkbox-group name="" @change="changeFileHandle">
				<view class="file_item" @longpress="showDel = true" v-for="(item,index) in fileList" :key="item.id">
					<view class="icon">
						<image src="/static/images/print/word.png" mode="scaleToFill"></image>
					</view>
					<view class="file_msg">
						<view class="file_name">{{item.fileName}}</view>
						<view class="time_size">{{item.gmtCreate}} {{item.fileSize}}KB</view>
					</view>
					<view v-if="showDel" class="file_radio">
						<label class="checkbox">
							<checkbox :value="item.id" />
						</label>
					</view>
				</view>
			</checkbox-group>
		</view>
		<view class="btn_box upload_btn" :class="{'hideUp':showDel}">
			<button type="default" class="btn_pri" @click="toGetFile">上传文件</button>
		</view>
		<view class="btn_box del_btn" :class="{'showDel':showDel}">
			<button size="mini" class="mini_btn" @click="showDel = false">取消</button>
			<button size="mini" class="mini_btn" type="warn" @click="delFile">删除</button>
		</view>
		<tmcModal :isShow="showMcModal" :showCancel="false" :content="content" title="温馨提示" @ok='ok'></tmcModal>
	</view>
</template>

<script>
	let _this;
	import {
		printList,
		queryStudentCardBalance,
		printRemove
	} from "../../../common/api/index.js";
	import {
		_showToast,
		_showJumpModal,
		_showLoading
	} from "../../../common/js/util.js";
	import tmcModal from '../../../components/custom-modal/mc-modal.vue'
	export default {
		data() {
			return {
				geListStu: [],
				fileList: [],
				showDel: false,
				delFileList: [],
				capacity: 0,
				showMcModal: false,
				content: ''
			}
		},
		components: {
			tmcModal
		},
		onShow() {
			console.log('onShow')
			_this._queryStudentCardBalance()
		},
		onLoad() {
			console.log('onLoad')
			_this = this;
		},
		watch: {
			showDel(newVal, oldVal) {
				_this.delFileList = []
			}
		},
		methods: {
			ok() {
				this.showMcModal1 = false
				uni.navigateBack({
					delta: 1
				})
			},
			toGetFile() {
				uni.navigateTo({
					url: './changFile'
				})
			},
			changeFileHandle(e) {
				_this.delFileList = e.detail.value
			},
			delFile() {
				if(_this.delFileList.length <= 0){
					_showToast('请勾选需要删除的文件');
					return
				}
				_showLoading('删除文件中...')
				printRemove({
					"ids": _this.delFileList.join()
				}).then(res => {
					if (res.code == 0) {
						setTimeout(() => {
							uni.hideLoading()
							_this._getFileList()
							_this.showDel = false
							_showToast('删除成功')
						}, 500)
					} else {
						setTimeout(() => {
							uni.hideLoading()
							_showToast('删除失败')
						}, 500)
					}
				})
			},
			_queryStudentCardBalance() {
				let s_userId = uni.getStorageSync("s_userId");
				queryStudentCardBalance({
						userId: s_userId,
						queryCard: true,
						queryWallet: true,
						bindType: 0
					}).then(res => {
						if (res.code == 0) {
							_this.geListStu = res.data.students;
							_this._getFileList()
						} else if(res.code == 1){
							_this.showMcModal = true,
							_this.content = '还没有绑定档案，无法上传文件，请先绑定档案（校园卡）'
						}else {
							_this.geListStu = [];
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
			_getFileList() {
				_showLoading('文件加载中...')
				printList({
					empId: _this.geListStu[0].students[0].empId
				}).then(res => {
					if (res.code == 0) {
						_this.fileList = res.rows
						_this.capacity = (res.fileCountSize / 1024).toFixed(2)
						setTimeout(() => {
							uni.hideLoading()
						}, 500)
					} else {
						_this.fileList = [];
					}
				})
			},
			toHistory(){
				uni.navigateTo({
					url: `./printHistory?orgId=${_this.geListStu[0].students[0].orgId}&empId=${_this.geListStu[0].students[0].empId}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.head {
		padding: 0 20rpx;
		height: 70rpx;
		box-shadow: 0 0 20px 1px #e8e8e8;
		font-size: 24rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		.history{
			color: #169BD5;
		}
	}

	.file_list {
		height: calc(100vh - 70rpx);
		padding-bottom: 160rpx;
		overflow: auto;
		box-sizing: border-box;

		.file_item {
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			align-items: center;
			padding: 20rpx 10rpx;

			.icon,
			.file_radio {
				width: 100rpx;
				height: 100rpx;
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
			}

			.icon {
				image {
					width: 82rpx;
					height: 82rpx;
				}
			}

			.file_msg {
				width: calc(100vw - 200rpx);

				.file_name,
				.time_size {
					padding: 6rpx;
				}

				.time_size {
					font-size: 24rpx;
					color: #999;
				}
			}
		}
	}

	.btn_box {
		position: fixed;
		width: 100vw;
		bottom: 36rpx;
		transition: all 0.5s;
		opacity: 1;

		&.del_btn {
			height: 100rpx;
			display: flex;
			flex-direction: row;
			justify-content: flex-end;
			align-items: center;
			bottom: -100rpx;
			opacity: 0;
			background-color: #FFF;
			box-shadow: 0 0 20px 1px #e8e8e8;

			.mini_btn {
				margin: 20rpx;
			}
		}

		&.hideUp {
			opacity: 0;
			bottom: -100rpx;
		}

		&.showDel {
			opacity: 1;
			bottom: 0;
		}

		.btn_pri {
			margin-top: 60rpx;
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			width: 90%;
			font-size: 32rpx;
			font-family: "PingFangSC-Heavy, PingFang SC";
			font-weight: 600;
			color: #ffffff;
			background: linear-gradient(90deg, #0685ff 0%, #285eff 100%);
			box-shadow: 0px 8px 16px 0px rgba(36, 98, 255, 0.24);
			border-radius: 8rpx;
		}
	}
</style>
